import React, {Component} from 'react';
import {View} from 'react-native';
import dva from './dva';

let app;

function init({
	initialState = {},
	models = [],
//	router = {},
	start = {},
	routeConfig = {},
	plugins = []}) {
	app = dva({
	    initialState: initialState,
	    models: models,
	    plugins: plugins,
	});
	// 启动
	return app.start(start);
}

function getStore() {
	if(app) {
		return app.getStore();
	}
}

function getState() {
	let currentState;
	let store = getStore();
	if(store) currentState = store.getState();
	return currentState;
}

function dispatch(...args) {
	let store = getStore();
	if(store) {
		store.dispatch(...args);
	}
}

function dispatchAll(fnName, payload) {
	let store = getStore();
	let currentState = store.getState();
	for(let key in currentState) {
		store.dispatch({
			type: key + '/' + fnName,
			payload: payload,
		});
	}
}

function resetAll() {
	dispatchAll('reset');
}

export {
	init,
	getStore,
	getState,
	dispatch,
	dispatchAll,
	resetAll,
};